'''
Created on 18.11.2009

@author: guest
'''
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import db
from google.appengine.ext import webapp
from ru.simpleprojecttool.db.model import Role
from ru.simpleprojecttool.utils import utils

    
class ListRoles(webapp.RequestHandler):
    def get(self):
        if self.request.get('projectId'):
            project = db.get(db.Key(self.request.get('projectId')))
            if utils.canEditProject(project) == False:
                utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
                return
            roles = Role.all().filter("project = ", project) 
            template_values = {
                               'roles': roles, 
                               'project': project
                               }
            
#            path = os.path.join(os.path.dirname(__file__), '../../../templates/role/roles.html')
#            self.response.out.write(template.render(path, template_values))
            utils.showTemplate(self, '../../../templates/role/roles.html', [], template_values)

class CreateRoleForm(webapp.RequestHandler):
    def get(self):
#        path = os.path.join(os.path.dirname(__file__), '../../../templates/role/createRole.html')
        template_values = {'projectId': self.request.get('projectId')}
        project = db.get(db.Key(self.request.get('projectId')))
        if utils.canEditProject(project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return

#        self.response.out.write(template.render(path, template_values))
        utils.showTemplate(self, '../../../templates/role/createRole.html', [], template_values)

        
class CreateRole(webapp.RequestHandler):
    def post(self):
        project = db.get(db.Key(self.request.get('projectId')))
        if utils.canEditProject(project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        role = Role(name = self.request.get('roleName'), 
                    project = project, 
                    canEditProject = (self.request.get('canEditProject') == 'on'), 
                    canAssign = (self.request.get('canAssign') == 'on'),
                    canBeAssigned = (self.request.get('canBeAssigned') == 'on'))
        role.put()
        self.redirect('/roles?projectId=%s' % (self.request.get('projectId')))

class DeleteRole(webapp.RequestHandler):
    def post(self):
        role = db.get(db.Key(self.request.get('roleId')))
        projectId = str(role.project.key()) ;
        if utils.canEditProject(role.project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        db.delete(role)
        self.redirect('/roles?projectId=%s' % projectId)

class EditRoleForm(webapp.RequestHandler):
    def get(self):
        role = db.get(db.Key(self.request.get('roleId')))
        if utils.canEditProject(role.project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        template_values = {
                           'role': role
                           }
        utils.showTemplate(self, '../../../templates/role/editRole.html', [], template_values)

        
class SaveRole(webapp.RequestHandler):
    def post(self):
        role = db.get(db.Key(self.request.get('roleId')))
        if utils.canEditProject(role.project) == False:
            utils.showTemplate(self, '../../../templates/error.html', [utils.Error(message = 'You can\'t acces this page', level = 'error')], {})
            return
        role.name = self.request.get('roleName')
        role.canEditProject = self.request.get('canEditProject') == 'on'
        role.canAssign = self.request.get('canAssign') == 'on'
        role.canBeAssigned = self.request.get('canBeAssigned') == 'on'
        role.put()
        self.redirect('/roles?projectId=%s' % str(role.project.key()))
